package com.aat.dxfy.buddy.test02;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class Test01 {

	public static void main(String[] args) {
		Framework f=new HardcodedFrameworkImpl();
		Service s =f.secure(Service.class);
		s.deleteEverything();
	}

}

@Retention(RetentionPolicy.RUNTIME)
@interface Secured {
	String user();
}

class UserHolder {
	static String user;
}

interface Framework {
	<T> T secure(Class<T> type);
}

class Service {
	void deleteEverything() {
		System.out.println("deleteEverything");
	}
}

class SecuredService extends Service {
	@Override
	void deleteEverything() {
		if (UserHolder.user.equals("ADMIN")) {
			super.deleteEverything();
		} else {
			throw new IllegalStateException("Not authorized");
		}
	}
}

class HardcodedFrameworkImpl implements Framework {
	@Override
	public <T> T secure(Class<T> cls) {
		if (cls == Service.class) {
			return cls.cast(new SecuredService());
		} else {
			throw new IllegalArgumentException("Unknown: " + cls);
		}
	}
}

